﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using caedisi.Web.DataModel;
using System.ComponentModel;
using caedisi.Helpers;

namespace caedisi.Views.Controls
{
    /// <summary>
    /// A control that exposes a cell
    /// </summary>
    public partial class BasicCellControl : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BasicCellControl"/> class.
        /// </summary>
        public BasicCellControl()
        {
            InitializeComponent();            
        }

        private CellStateType _SelectedCellStateType;
        /// <summary>
        /// Gets or sets the current cellular automata.
        /// </summary>
        /// <value>
        /// The current cellular automata.
        /// </value>
        public CellStateType SelectedCellStateType
        {
            get
            {
                return _SelectedCellStateType;
            }
            set
            {
                if (_SelectedCellStateType != value)
                {
                    _SelectedCellStateType = value;
                    this.RaisePropertyChanged("SelectedCellStateType");
                }
            }
        }

        /// <summary>
        /// Gets or sets the cell state type id.
        /// </summary>
        /// <value>
        /// The cell state type id.
        /// </value>
        public int? SelectedCellStateTypeId
        {
            get { return (int?) GetValue(SelectedCellStateTypeIdEntityProperty); }
            set
            {
                SetValue(SelectedCellStateTypeIdEntityProperty, value);
            }
        }

        /// <summary>
        /// a dependency property for the CellStateTypeId
        /// </summary>
        public static readonly DependencyProperty SelectedCellStateTypeIdEntityProperty =
                DependencyProperty.Register(
                "SelectedCellStateTypeId",
                typeof(int?),
                typeof(BasicCellControl),
                new PropertyMetadata(OnCellStateTypeIdChanged));


        /// <summary>
        /// Called when [cell state type id changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnCellStateTypeIdChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            int? selectedCellStateTypeId = args.NewValue as int?;
            BasicCellControl transitionRuleControl = sender as BasicCellControl;
                
            if (selectedCellStateTypeId.HasValue)
            {
                transitionRuleControl.SelectedCellStateType = EntityHelper.GetCellStateType(selectedCellStateTypeId.Value);
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
